package com.kk.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 消息拒收，测试加入死信
 */
@Component
public class DLXListener {

    @RabbitListener(queues = "QA")
    public void receiveMessage1(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag)  {

        try {
            //1.获取消息
            System.out.println (new String (message.getBody ( )));
            //2. 处理业务逻辑
            int a = 1/0;
            //3. 手动签收
            channel.basicAck (tag, true);
        } catch (Exception e) {
//            e.printStackTrace ( );
            System.out.println ("出现异常，拒绝接受");
            //4.拒绝签收，不重回队列 requeue=false
            try {
                channel.basicNack (tag, true, false);
            } catch (IOException e1) {
                e1.printStackTrace ( );
            }
        }

    }
}
